• Exception handling using catch-and-throw (Open Prolog 1.0d37 and later).
<Information to be added. In the meantime, consult the ISO Prolog draft>
• Exception Handling in built-in predicates in Open Prolog
Whenever an error is discovered in a built-in, an error term is thrown. If no catch/3 predicate intervenes, the error term will be caught by the default catcher which will output a suitable error message and abort the program.
The form of the term that's thrown is: error(Kind,ListOfInformation).
The Kind is one of the ISO Prolog Draft errors, plus two more. Here is the list of all of them:
unclassified_error %this is not part of ISO
user_interrupt %this is not part of ISO
system_error %a system_error can't be caught by a catch/3 predicate.
calculation_error
database_error
evaluation_error
implementation_error
instantiation_error
io_control_error
io_end_of_file_error
io_formatting_error
operator_error
overflow_error
domain_error %was range_error
syntax_error
type_error
undefined_predicate_error
undefined_value_error
underflow_error
zero_divide_error
At the moment, the majority of built-ins report an unclassified_error. As time goes by, they will be correctly classified.
To see some errors being thrown and caught, try the following predicates:
tab(_).
tab(a).
tab(-1).
listing(_).
see('non existent file').
tell('non existent volume:file').
X is a.
The ListOfInformation may contain auxiliary information that might be helpful to an error handler, or it could be empty. Auxiliary information will be of the form <category>(Information). This scheme is flexible, in that the list can contain anything remotely useful, and the error handler can access anything it understands while ignoring the rest. As Open Prolog evolves, the auxiliary information may increase. The following terms are used at present:
argument_index(OffendingArgumentNumber) %starting at 1
argument(OffendingArgument). Variables in arguments may be instantiated as a result of the partial execution of the built-in.
goal(Call). This is the goal in which the exception occured. Terms that were variables at the start of the call may be instantiated as a result of the partial execution of the built-in.
If the Call has very complex arguments (e.g. as a result of an occur check violation), the error handler may itself hang up. If you abort under these circumstances, you lose the error information - sorry.
error_code(ErrorCode). This will be an Open Prolog error code. The built-in 'get$prolog$error$message'(Code,Message) can be used to retrieve the error message.
Example:
'get$prolog$error$message'(-32767,Message).
error_message(ErrorMessageAtom).
host_error_code(MacErrorCodeNumber). This will be a standard Macintosh error code. The built-in 'get$host$error$message'(Code,Message) can be used to retrieve the error message.
Example:
'get$host$error$message'(-39,Message).
host_error_message(MessageAtom). This will be a string of text from the Macintosh.
If you are building an error exception catcher, please do not assume that the order of appearance of auxiliary information in the list will remain stable during program execution, or even that the same amount of information will result each time the same error occurs. Furthermore, the amount and order of information will change as Open Prolog evolves, although the standard <category>s will remain the same, as far as possible.